{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9.22 \u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684\u7b80\u5355\u65b9\u6cd5\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u81ea\u5df1\u53bb\u5b9e\u73b0\u4e00\u4e2a\u65b0\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u4ee5\u4fbf\u4f7f\u7528with\u8bed\u53e5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9e\u73b0\u4e00\u4e2a\u65b0\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528 contexlib \u6a21\u5757\u4e2d\u7684 @contextmanager \u88c5\u9970\u5668\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86\u4ee3\u7801\u5757\u8ba1\u65f6\u529f\u80fd\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import time\nfrom contextlib import contextmanager\n\n@contextmanager\ndef timethis(label):\n    start = time.time()\n    try:\n        yield\n    finally:\n        end = time.time()\n        print('{}: {}'.format(label, end - start))\n\n# Example use\nwith timethis('counting'):\n    n = 10000000\n    while n > 0:\n        n -= 1"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u51fd\u6570 timethis() \u4e2d\uff0cyield \u4e4b\u524d\u7684\u4ee3\u7801\u4f1a\u5728\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4e2d\u4f5c\u4e3a __enter__() \u65b9\u6cd5\u6267\u884c\uff0c\n\u6240\u6709\u5728 yield \u4e4b\u540e\u7684\u4ee3\u7801\u4f1a\u4f5c\u4e3a __exit__() \u65b9\u6cd5\u6267\u884c\u3002\n\u5982\u679c\u51fa\u73b0\u4e86\u5f02\u5e38\uff0c\u5f02\u5e38\u4f1a\u5728yield\u8bed\u53e5\u90a3\u91cc\u629b\u51fa\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u52a0\u9ad8\u7ea7\u4e00\u70b9\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u5b9e\u73b0\u4e86\u5217\u8868\u5bf9\u8c61\u4e0a\u7684\u67d0\u79cd\u4e8b\u52a1\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@contextmanager\ndef list_transaction(orig_list):\n    working = list(orig_list)\n    yield working\n    orig_list[:] = working"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6bb5\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u4efb\u4f55\u5bf9\u5217\u8868\u7684\u4fee\u6539\u53ea\u6709\u5f53\u6240\u6709\u4ee3\u7801\u8fd0\u884c\u5b8c\u6210\u5e76\u4e14\u4e0d\u51fa\u73b0\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\u624d\u4f1a\u751f\u6548\u3002\n\u4e0b\u9762\u6211\u4eec\u6765\u6f14\u793a\u4e00\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "items = [1, 2, 3]\nwith list_transaction(items) as working:\n    working.append(4)\n    working.append(5)\nitems"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with list_transaction(items) as working:\n    working.append(6)\n    working.append(7)\n    raise RuntimeError('oops')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "items"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u8981\u5199\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u91cc\u9762\u5305\u542b\u4e00\u4e2a __enter__() \u548c\u4e00\u4e2a\n__exit__() \u65b9\u6cd5\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import time\n\nclass timethis:\n    def __init__(self, label):\n        self.label = label\n\n    def __enter__(self):\n        self.start = time.time()\n\n    def __exit__(self, exc_ty, exc_val, exc_tb):\n        end = time.time()\n        print('{}: {}'.format(self.label, end - self.start))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u8fd9\u4e2a\u4e5f\u4e0d\u96be\u5199\uff0c\u4f46\u662f\u76f8\u6bd4\u8f83\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528 @contextmanager \u6ce8\u89e3\u7684\u51fd\u6570\u800c\u8a00\u8fd8\u662f\u7a0d\u663e\u4e4f\u5473\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "@contextmanager \u5e94\u8be5\u4ec5\u4ec5\u7528\u6765\u5199\u81ea\u5305\u542b\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u51fd\u6570\u3002\n\u5982\u679c\u4f60\u6709\u4e00\u4e9b\u5bf9\u8c61(\u6bd4\u5982\u4e00\u4e2a\u6587\u4ef6\u3001\u7f51\u7edc\u8fde\u63a5\u6216\u9501)\uff0c\u9700\u8981\u652f\u6301 with \u8bed\u53e5\uff0c\u90a3\u4e48\u4f60\u5c31\u9700\u8981\u5355\u72ec\u5b9e\u73b0\n__enter__() \u65b9\u6cd5\u548c __exit__() \u65b9\u6cd5\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}